package com.sf.service.impl;

import com.sf.domain.Car;
import com.sf.mapper.CarMapper;
import com.sf.qo.QueryObject;
import com.sf.service.ICarService;
import com.sf.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CarServiceImpl implements ICarService {
    @Autowired
    private CarMapper carMapper;


    @Override
    public PageResult query(QueryObject queryObject) {
        // 如何去判断数据库当中是否有数据
        // 1 查询数据库当中总条数, 如果为0 表述数据库中没有数据
        Integer count = carMapper.selectForCount();
        if(count == 0 ){
            return new PageResult(queryObject.getCurrentPage(),
                    queryObject.getPageSize(),
                    Collections.emptyList(),
                    0);
        }
        // 2 如果有数据进行分页
        List<Car> list =  carMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }

}
